<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

abstract class CreateTables
{
	/**
	 * Nastaví počiatočné dáta
	 *
	 * @return void
	 */
	static private function data()
	{
		$lang    = $_SESSION['language'];
		$general = $_SESSION['general'];

		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('skin', 'default/')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('skin_admin', 'default.css')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('h1', '".db::escape($general['h1'])."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('title', '".db::escape($general['title'])."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('language', '".db::escape($lang)."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('description', '".db::escape($general['description'])."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('keywords', '".db::escape($general['keywords'])."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('cookie_nameprefix', '".db::escape($general['cookie_nameprefix'])."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('cookie_expiration', '".db::escape($general['cookie_expiration'])."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('cookie_path', '".db::escape($general['cookie_path'])."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('cookie_domain', '".db::escape($general['cookie_domain'])."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('cookie_secure', '".($general['cookie_secure'] == 'true' ? '1' : '0')."')");
		db::query("INSERT INTO `".db::prefix('configuration')."` VALUES ('cookie_httponly', '".($general['cookie_httponly'] == 'true' ? '1' : '0')."')");
		db::query("INSERT INTO `".db::prefix('languages')."` VALUES (1, 'sk');");
		db::query("INSERT INTO `".db::prefix('languages')."` VALUES (2, 'en');");
		db::query("INSERT INTO `".db::prefix('members')."` VALUES ('1', 'admin', '8450eca01665516d9aeb5317764902b78495502637c96192c81b1683d32d691a0965cf037feca8b9ed9ee6fc6ab8f27fce8f77c4fd9b4a442a00fc317b8237e6', 1, 0);");
		db::query("INSERT INTO `".db::prefix('members_groups')."` VALUES (1, 'Admin');");
		db::query("INSERT INTO `".db::prefix('members_groups')."` VALUES (2, 'Members');");
		db::query("INSERT INTO `".db::prefix('skins')."` VALUES ('index', 'application/xhtml+xml', 'default')");
	}

	/**
	 * Vytvoriť tabuľky
	 *
	 * @return void
	 */
	static public function begin()
	{
		$dump = file_get_contents(dirname(__FILE__).'/mysql.sql');
		$dump = explode("\n",trim($dump));

		// počet chýb vykonaných pri obnove
		$errors  = 0;

		// dočasné uloženie SQL príkazu
		$tmp     = NULL;

		// SQL príkazy
		$queries = array();

		// rozdeliť SQL príkazy a uložiť ich do poľa
		foreach($dump as $line) {
		    // vynechať komentáre
			if (!preg_match('/^(-|#)/',$line)) {
				$tmp .= $line;
				if (preg_match('/(.*);$/',$line)) {
					$queries[] = $tmp; // uložiť SQL príkaz
					$tmp = NULL; 
				}
			}
		}

		// vykonať zálohu
		foreach ($queries as $query) {
			// pridať prefix
			$prefix = $_SESSION['database']['prefix'];

			// pridať prefixy
			$query  = preg_replace(
			"/^CREATE\s+TABLE\s+([a-z][a-z0-9_`]+)\s+\(/i",
			"CREATE TABLE `{$prefix}$1` (",
			$query);

			$query  = preg_replace(
			"/^DROP\s+TABLE\s+IF\s+EXISTS\s+([a-z][a-z0-9_`]+)\s*;/i",
			"DROP TABLE IF EXISTS `{$prefix}$1`;",
			$query);

			if(!db::query($query)) $errors++;
			#echo _hsc($query)."<br /><br />\n"; // DEBUG
		}

		// vložiť dáta
		self::data();

		// vyhodnotiť akciu
		if (count($queries) > 0) {
			if ($errors > 0) {
				throw new Exception('Database was created with following number of errors: '.$errors);
			}
		}
	}
}
